
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <stdio.h>

#define MAX_LINE 64

// parent pipe -> child
// fd (0=stdin, 1=stdout, 2=stderr)
// man pipe
 // pipe()  creates  a pipe, a unidirectional data channel that can be used
 //       for interprocess communication.  The array pipefd is used to return two
 //       file  descriptors  referring to the ends of the pipe.  pipefd[0] refers
 //       to the read end of the pipe.  pipefd[1] refers to the write end of  the
 //       pipe.   Data  written  to  the write end of the pipe is buffered by the
 //       kernel until it is read from the read end of  the  pipe
int main(int argc, char **argv){

	pid_t pid;
	int fd[2];

	if(pipe(fd) <0){
		perror("pipe error");
		return 1;
	}

	if((pid = fork())<0){
		perror("fork error");
		return 1;
	}

	if(pid == 0){	// child
		close(fd[1]);
		char line[MAX_LINE];
		int n = read(fd[0],line,MAX_LINE);
		write(STDOUT_FILENO, line,n);
	}else{	// parent
		close(fd[0]);
		char* str="hello world\n";
		write(fd[1], str, strlen(str));
	}

	return 0;
}